//
//  DrivingLicenseCertificationViewController.m
//  UcarTest
//
//  Created by 陈曦 on 16/3/9.
//  Copyright © 2016年 chenxi. All rights reserved.
//

#import "DrivingLicenseCertificationViewController.h"
#import "STPhotoKitController.h"
#import "UIImagePickerController+ST.h"
#import "STConfig.h"
#import "Define.h"
#import "AFNetworking.h"
#import "NetRequestGroup.h"
#import "Access_token.h"
#import "MBProgressHUD.h"

typedef NS_ENUM(NSInteger, PhotoType)
{
    PhotoTypeIcon,
    PhotoTypeRectangle,
};

@interface DrivingLicenseCertificationViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate, UIActionSheetDelegate, STPhotoKitDelegate>

@property (nonatomic, strong) UIScrollView *bgContainerView;//创建背景scroll视图

@property (nonatomic, assign) PhotoType type;


@end

@implementation DrivingLicenseCertificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //导航条
    self.navigationController.navigationBarHidden = NO;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"驾照认证";
    self.navigationItem.titleView = titleLabel;
    self.navigationController.navigationBar.barTintColor = mainColor;
    self.view.backgroundColor = backgroundMainColor;
    
    //重定义返回键
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(0, 0, 22, 22);
    [back setImage:[UIImage imageNamed:@"iconfont-fanhui-white"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backTpMainView) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backBar;
    
    //创建scrollView
    self.bgContainerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 48)];
    self.bgContainerView.backgroundColor = backgroundMainColor;
    self.bgContainerView.contentSize = CGSizeMake(kScreenWidth, 220 + (kScreenWidth - 76) * 1.25);
    [self.view addSubview:self.bgContainerView];
    
    //添加视图
    [self UploadIDPhoto];

    
}

- (void) UploadIDPhoto {
    
    //第一部分-------------------------------------------------------------------
    //正面照片上传
    UIView *upIDImageView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, kScreenWidth - 20, (kScreenWidth - 76) / 1.6 + 75)];
    upIDImageView.backgroundColor = [UIColor whiteColor];
    upIDImageView.layer.shadowOpacity = 1;
    upIDImageView.layer.shadowColor = [UIColor grayColor].CGColor;
    upIDImageView.layer.shadowRadius = 2.0;
    upIDImageView.layer.cornerRadius = 2.0;
    upIDImageView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
    [self.bgContainerView addSubview:upIDImageView];
    
    //证件小图标
    UIImageView *IDIconImageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 15)];
    IDIconImageV.image = [UIImage imageNamed:@"icon_id_jiazhao"];
    [upIDImageView addSubview:IDIconImageV];
    
    //驾照标签
    UILabel *IDShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, 120, 15)];
    IDShowLabel.text = @"驾照 (主页)";
    IDShowLabel.textAlignment = NSTextAlignmentLeft;
    IDShowLabel.textColor = [UIColor blackColor];
    [upIDImageView addSubview:IDShowLabel];
    
    UIView *IDShowDemarcationLine = [[UIView alloc] initWithFrame:CGRectMake(10, 35, kScreenWidth - 40, 0.5)];//分界线
    IDShowDemarcationLine.backgroundColor = lineColor;
    [upIDImageView addSubview:IDShowDemarcationLine];
    
    UILabel *touchUpInsideUplodingUpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ((kScreenWidth - 76) / 1.6 + 75) / 2 - 8, kScreenWidth - 20, 16)];
    touchUpInsideUplodingUpLabel.text = @"点击上传驾照主页图";
    touchUpInsideUplodingUpLabel.textColor = mainColor;
    touchUpInsideUplodingUpLabel.textAlignment = NSTextAlignmentCenter;
    touchUpInsideUplodingUpLabel.font = [UIFont boldSystemFontOfSize:16];
    [upIDImageView addSubview:touchUpInsideUplodingUpLabel];
    
    //上传按钮
    self.upImageUploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.upImageUploadBtn.frame = CGRectMake(28, 50, kScreenWidth - 76, (kScreenWidth - 76) / 1.6);
    [self.upImageUploadBtn addTarget:self action:@selector(uploadUpIDImage) forControlEvents:UIControlEventTouchUpInside];
    [upIDImageView addSubview:self.upImageUploadBtn];
    
    //第二部分-------------------------------------------------------------------
    UIView *downIDImageView = [[UIView alloc] initWithFrame:CGRectMake(10, 25 + (kScreenWidth - 76) / 1.6 + 75, kScreenWidth - 20, (kScreenWidth - 76) / 1.6 + 75)];
    downIDImageView.backgroundColor = [UIColor whiteColor];
    downIDImageView.layer.shadowOpacity = 1;
    downIDImageView.layer.shadowColor = [UIColor grayColor].CGColor;
    downIDImageView.layer.shadowRadius = 2.0;
    downIDImageView.layer.cornerRadius = 2.0;
    downIDImageView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
    [self.bgContainerView addSubview:downIDImageView];
    
    //证件小图标
    UIImageView *downIDIconImageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 15)];
    downIDIconImageV.image = [UIImage imageNamed:@"icon_id_jiazhao"];
    [downIDImageView addSubview:downIDIconImageV];
    
    //驾照标签
    UILabel *downIDShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, 120, 15)];
    downIDShowLabel.text = @"驾照 (副页)";
    downIDShowLabel.textAlignment = NSTextAlignmentLeft;
    downIDShowLabel.textColor = [UIColor blackColor];
    [downIDImageView addSubview:downIDShowLabel];
    
    UIView *downIDShowDemarcationLine = [[UIView alloc] initWithFrame:CGRectMake(10, 35, kScreenWidth - 40, 0.5)];//分界线
    downIDShowDemarcationLine.backgroundColor = lineColor;
    [downIDImageView addSubview:downIDShowDemarcationLine];
    
    UILabel *touchUpInsideUplodingDownLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ((kScreenWidth - 76) / 1.6 + 75) / 2 - 8, kScreenWidth - 20, 16)];
    touchUpInsideUplodingDownLabel.text = @"点击上传驾照副页图片";
    touchUpInsideUplodingDownLabel.textColor = mainColor;
    touchUpInsideUplodingDownLabel.textAlignment = NSTextAlignmentCenter;
    touchUpInsideUplodingDownLabel.font = [UIFont boldSystemFontOfSize:16];
    [downIDImageView addSubview:touchUpInsideUplodingDownLabel];
    
    //上传按钮
    self.downImageDownloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.downImageDownloadBtn.frame = CGRectMake(28, 50, kScreenWidth - 76, (kScreenWidth - 76) / 1.6);
    [self.downImageDownloadBtn addTarget:self action:@selector(uploadDownIDImage) forControlEvents:UIControlEventTouchUpInside];
    [downIDImageView addSubview:self.downImageDownloadBtn];
    
    //第三部分-------------------------------------------------------------------
    //立即租用按钮
    self.applicationCertificationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.applicationCertificationBtn.frame = CGRectMake(20, kScreenHeight - 41, kScreenWidth - 40, 34);
    self.applicationCertificationBtn.layer.cornerRadius = 3;
    self.applicationCertificationBtn.layer.masksToBounds = YES;
    self.applicationCertificationBtn.backgroundColor = mainColor;
    [self.applicationCertificationBtn setTitle:@"申请认证" forState:UIControlStateNormal];
    
    self.applicationCertificationBtn.titleLabel.tintColor = [UIColor whiteColor];
    [self.applicationCertificationBtn addTarget:self action:@selector(UploadIDPhotoPush) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.applicationCertificationBtn];
    
    UIView *applicationCertificationDemarcationLine = [[UIView alloc] initWithFrame:CGRectMake(0,  kScreenHeight - 48, kScreenWidth, 0.5)];//分界线
    applicationCertificationDemarcationLine.backgroundColor = lineColor;
    [self.view addSubview:applicationCertificationDemarcationLine];
    
}

#pragma mark 正面照片收录
- (void) uploadUpIDImage {
    
    self.type = PhotoTypeIcon;
    
    [self editImageSelected];
    
}

#pragma mark 反面照片收录
- (void) uploadDownIDImage {
    
    self.type = PhotoTypeRectangle;
    
    [self editImageSelected];
    
}

#pragma mark - --- event response 事件相应 ---
- (void)editImageSelected
{
    UIAlertController *alertController = [[UIAlertController alloc]init];
    
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypeCamera];
        
        if ([controller isAvailableCamera] && [controller isSupportTakingPhotos]) {
            [controller setDelegate:self];
            [self presentViewController:controller animated:YES completion:nil];
        }else {
        }
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [controller setDelegate:self];
        if ([controller isAvailablePhotoLibrary]) {
            [self presentViewController:controller animated:YES completion:nil];
        }    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alertController addAction:action0];
    [alertController addAction:action1];
    [alertController addAction:action2];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - 2.UIImagePickerController的委托

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *imageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage];
        STPhotoKitController *photoVC = [STPhotoKitController new];
        [photoVC setDelegate:self];
        [photoVC setImageOriginal:imageOriginal];
        switch (self.type) {
            case PhotoTypeIcon:
                [photoVC setSizeClip:CGSizeMake(kScreenWidth - 76, (kScreenWidth - 76) / 1.6)];
                break;
            case PhotoTypeRectangle:
                [photoVC setSizeClip:CGSizeMake(kScreenWidth - 76, (kScreenWidth - 76) / 1.6)];
                break;
            default:
                break;
        }
        
        [self presentViewController:photoVC animated:YES completion:nil];
        
    }];
}

- (void)photoKitController:(STPhotoKitController *)photoKitController resultImage:(UIImage *)resultImage {
    
    switch (self.type) {
        case PhotoTypeIcon:{
            [self.upImageUploadBtn setImage:resultImage forState:UIControlStateNormal];
            
            self.upImageUploadImage = resultImage;
            
            //设置一个图片的存储路径
            NSString *path_sandox = NSHomeDirectory();
            
            NSString *imagePath = [path_sandox stringByAppendingString:@"/Documents/UpIDImage.png"];
            //把图片直接保存到指定的路径（同时应该把图片的路径imagePath存起来，下次就可以直接用来取）
            [UIImagePNGRepresentation(resultImage) writeToFile:imagePath atomically:YES];
            
            UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:imagePath];
            
            NSData* imageData = UIImagePNGRepresentation(imgFromUrl3);
            
            self.upImageBase64Str = [imageData base64Encoding];
            
            
            [self UpImageNetworkRequests];

        }
            
            break;
            
        case PhotoTypeRectangle:
        {
            [self.downImageDownloadBtn setImage:resultImage forState:UIControlStateNormal];
            
            self.downImageUploadImage = resultImage;
            
            //设置一个图片的存储路径
            NSString *path_sandox = NSHomeDirectory();
            
            NSString *imagePath = [path_sandox stringByAppendingString:@"/Documents/DownIDImage.png"];
            //把图片直接保存到指定的路径（同时应该把图片的路径imagePath存起来，下次就可以直接用来取）
            [UIImagePNGRepresentation(resultImage) writeToFile:imagePath atomically:YES];
            
            UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:imagePath];
            
            NSData* imageData = UIImagePNGRepresentation(imgFromUrl3);
            
            self.downImageBase64Str = [imageData base64Encoding];
            
            [self DownImageNetworkRequests];
            
        }
            break;
        default:
            break;
    }
    
}


#pragma mark 网络请求
- (void) NetworkRequests{
    
    //初始化进度框，置于当前的View当中
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.label.text = @"正在加载";
    [HUD showAnimated:YES];//HUD开始
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    Access_token *useAccess = [Access_token shareInstance];
    
    //传入的参数
    NSDictionary *para = @{
                           @"type" : @"1",
                           @"access_token" : useAccess.access_token,
                           @"apply_type" : @"1",
                           @"upload_type" : self.drive_upload_type,
                           @"id" : self.drive_id,
                           @"img_src_front" : self.upImageUploadImageStr,
                           @"img_src_behind" : self.downImageUploadImageStr
                           };
    //你的接口地址
    NSString *url = [NSString stringWithFormat:@"%@%@", kRealmName,kNetIDDriver];
    //发送请求
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [HUD hideAnimated:YES];
        
        
        NSDictionary *dic = responseObject;
        
        self.status = dic[@"status"];
        
        self.msg = dic[@"msg"];
        
        [self resultAnalysis];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [HUD hideAnimated:YES];
        
        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有网络" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
    }];
    
}

#pragma mark 申请认证上传照片
//申请认证上传照片
- (void) UploadIDPhotoPush {
    
    if (self.upImageUploadImageStr) {
        if (self.downImageUploadImageStr) {
            
            [self NetworkRequests];
            
        } else {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有上传反面照" preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
    } else {
        
        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有上传正面照" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
        
        
    }
    
}

//返回上一页
- (void)backTpMainView{
    
    [self.navigationController popViewControllerAnimated:nil];
    
}



#pragma mark 返回值分析
- (void) resultAnalysis {
    
    int status = [self.status intValue];
    
    switch (status) {
        case 99:{
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 98:{
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 97:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 96:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 1:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 2:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 3:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 4:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 5:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 6:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 7:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
            
        case 100:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
                [self.navigationController popViewControllerAnimated:nil];
                
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
            
        default:
            break;
    }
    
    
    
}


#pragma mark 驾驶证正面网络请求
- (void) UpImageNetworkRequests{
    
    
    
    //初始化进度框，置于当前的View当中
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.label.text = @"正在加载";
    [HUD showAnimated:YES];//HUD开始
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    Access_token *useAccess = [Access_token shareInstance];
    
    //传入的参数
    NSDictionary *para = @{
                           @"type" : @"1",
                           @"access_token" : useAccess.access_token,
                           @"imageFile" : self.upImageBase64Str
                           };
    //你的接口地址
    NSString *url = [NSString stringWithFormat:@"%@%@", kRealmName, kNetUpdateImage];
    //发送请求
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [HUD hideAnimated:YES];
        
        
        NSDictionary *dic = responseObject;
        
        self.upImageStatus = dic[@"status"];
        
        self.upImageMsg = dic[@"msg"];
        
        NSDictionary *info = dic[@"info"];
        
        self.upImageUploadImageStr = info[@"image_src"];
        
        [self upImageResultAnalysis];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [HUD hideAnimated:YES];

        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有网络" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
    }];
    
    
    
}

#pragma mark 驾驶证反面网络请求
- (void) DownImageNetworkRequests{
    
    
    
    //初始化进度框，置于当前的View当中
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.label.text = @"正在加载";
    [HUD showAnimated:YES];//HUD开始
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    Access_token *useAccess = [Access_token shareInstance];
    
    //传入的参数
    NSDictionary *para = @{
                           @"type" : @"1",
                           @"access_token" : useAccess.access_token,
                           @"imageFile" : self.downImageBase64Str
                           };
    //你的接口地址
    NSString *url = [NSString stringWithFormat:@"%@%@", kRealmName, kNetUpdateImage];
    //发送请求
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [HUD hideAnimated:YES];
        
        
        NSDictionary *dic = responseObject;
        
        self.downImageStatus    = dic[@"status"];
        
        self.downImageMsg = dic[@"msg"];
        
        NSDictionary *info = dic[@"info"];
        
        self.downImageUploadImageStr = info[@"image_src"];
        
        [self downImageResultAnalysis];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [HUD hideAnimated:YES];

        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有网络" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
    }];
    
    
    
    
    
    
}

#pragma mark 正面照返回值分析
- (void) upImageResultAnalysis {
    
    int status = [self.upImageStatus intValue];
    
    switch (status) {
        case 99:{
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.upImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 98:{
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.upImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 97:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.upImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 96:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.upImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 1:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.upImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 100:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.upImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
                
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
            
        default:
            break;
    }
    
    
    
}

#pragma mark 正面照返回值分析
- (void) downImageResultAnalysis {
    
    int status = [self.downImageStatus intValue];
    
    switch (status) {
        case 99:{
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.downImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 98:{
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.downImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 97:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.downImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 96:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.downImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 1:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.downImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 100:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.downImageMsg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
            
        default:
            break;
    }
    
    
    
}

@end
